package com.hod.client.chargedescription;

import com.google.gwt.core.client.GWT;
import com.google.gwt.gears.client.database.DatabaseException;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.Widget;
import com.gwtext.client.core.EventObject;
import com.gwtext.client.data.Record;
import com.gwtext.client.widgets.Button;
import com.gwtext.client.widgets.event.ButtonListenerAdapter;
import com.gwtext.client.widgets.form.FieldSet;
import com.gwtext.client.widgets.form.FormPanel;
import com.gwtext.client.widgets.form.TextField;
import com.gwtext.client.widgets.grid.AbstractSelectionModel;
import com.gwtext.client.widgets.grid.RowSelectionModel;
import com.gwtext.client.widgets.grid.event.RowSelectionListenerAdapter;
import com.hod.client.HOD;
import com.hod.client.backend.orm.client.GearsDataStore;
import com.hod.client.backend.orm.client.GearsQuery;
import com.hod.client.frontend.CRUD;
import com.hod.client.frontend.HODForm;
import com.hod.client.lodging.Lodging;
import com.hod.client.reserves.ReservationState;
import com.hod.client.reserves.ReservationStateGrid;
import com.hod.client.room.RoomCRUD;

public class ChargeDescriptionCRUD extends CRUD {

    
    public static void createListBox(Integer idSelected,ListBox lb){
		GearsDataStore ds = (GearsDataStore) GWT.create(ChargeDescription.class);
		try {
			//Obtengo todos los hospedajes validos
			GearsQuery q = ds.getNamedQuery(ChargeDescription.FIND_ALL);
			ChargeDescription[] data = (ChargeDescription[]) q.execute();
			lb.clear();
			if (data.length>0){
				for (ChargeDescription cd:data){
					lb.addItem(cd.getShortDescription(),String.valueOf(cd.getIdChargeDescription()));
					if (idSelected.equals(cd.getIdChargeDescription())){
						lb.setSelectedIndex(lb.getItemCount()-1);
					}
				}
			}else{
				lb.addItem("No hay descripciones cargadas");
			}
		} catch (DatabaseException e) {			
			// TODO Auto-generated catch block
		}
		
	}
    
	@Override
	protected void buildPanelSearch() {
		// TODO Auto-generated method stub

	}

	@Override
	protected AbstractSelectionModel createSelectionModel() {
		// TODO Auto-generated method stub
		final RowSelectionModel sm = new RowSelectionModel(true);
        sm.addListener(new RowSelectionListenerAdapter() {
        	public void onRowSelect(RowSelectionModel sm, int rowIndex, Record record) {
                formPanel.updateDataControls(record);             
            }
        });
        return sm;
	}

	@Override
	protected Widget formTabs() {
		// TODO Auto-generated method stub
		return null;
	}
	public ChargeDescriptionCRUD() {
		ds = (GearsDataStore) GWT.create(ChargeDescription.class);
		gridPanel = new ChargeDescriptionGrid(false,ds);				
	}

	@Override
	public HODForm createFormPanel() {
		// TODO Auto-generated method stub
		if (formPanel==null) formPanel = new ChargeDescriptionFormPanel(ds);
		return formPanel;
	}

}
